<?php
/**
 * cache.func.php
 */

if(!defined('IN_SITE')) {
	exit('Access Denied');
}

function updatecache($cachename = '')
{
	global $db, $tablepre;
	
	static $cachescript = array(
		'settings'	=> array('settings'),
	);
	
	$updatelist = empty($cachename) ? array_values($cachescript) : (is_array($cachename) ? array('0' => $cachename) : array(array('0' => $cachename)));
	$updated = array();
	foreach($updatelist as $value)
	{
		foreach($value as $cname)
		{
			if(empty($updated) || !in_array($cname, $updated))
			{
				$updated[] = $cname;
				getcachearray($cname);
			}
		}
	}
	
	foreach($cachescript as $script => $cachenames)
	{
		if(empty($cachename) || (!is_array($cachename) && in_array($cachename, $cachenames)) || (is_array($cachename) && array_intersect($cachename, $cachenames))) {
			$cachedata = '';
			$query = $db->query("SELECT data FROM {$tablepre}caches WHERE cachename in(".implodeids($cachenames).")");
			while($data = $db->fetch_array($query))
			{
				$cachedata .= $data['data'];
			}
			writetocache($script, $cachenames, $cachedata);
		}
	}

}

function writetocache($script, $cachenames, $cachedata = '', $prefix = 'cache_')
{
	if(is_array($cachenames) && !$cachedata)
	{
		foreach($cachenames as $name)
		{
			$cachedata .= getcachearray($name, $script);
		}
	}

	if(!is_dir(CACHE_PATH))
	{
		@mkdir(CACHE_PATH, 0777);
	}
	if($fp = @fopen(CACHE_PATH."$prefix$script.php", 'wb'))
	{
		fwrite($fp, "<?php\n//This is cache file, DO NOT modify!".
			"\n//Created: ".date("M j, Y, G:i").
			"\n//Identify: ".md5($prefix.$script.'.php'.$cachedata)."\n\n$cachedata?>");
		fclose($fp);
	} else {
		exit('Can not write to cache files, please check directory ./data/ and ./data/cache/ .');
	}
}

function getcachearray($cachename, $script = '')
{
	global $db, $timestamp, $tablepre, $charset;

	$cols = '*';
	$conditions = '';
	
	switch($cachename)
	{
		case 'settings':
			$table = 'settings';
			$conditions = "WHERE )";
			break;
			
		case 'recomarticle':
			$cols = 'title, stitle, description, islink, url, pubtime';
			$table = 'content';
			$zuijin = $timestamp-86400*30;
			$conditions = "WHERE `status` > 0 AND `pubtime` > {$zuijin} ORDER BY `status` DESC, `pubtime` DESC LIMIT 0, 10";
			break;

	}
	
	$data = array();
	if(empty($table) || empty($cols)) return '';
	$query = $db->query("SELECT $cols FROM {$tablepre}$table $conditions");
	
	switch ($cachename)
	{
		
			
		default:
			while ($row = $db->fetch_array($query)) {
				$data[] = $row;
			}
			break;
	}
	$curdata = "\$_SCACHE['$cachename'] = ".arrayeval($data).";\n\n";
	return $curdata;
}

function arrayeval($array, $level = 0)
{
	if(!is_array($array)) {
		return "'".$array."'";
	}
	if(is_array($array) && function_exists('var_export')) {
		return var_export($array, true);
	}

	$space = '';
	for($i = 0; $i <= $level; $i++) {
		$space .= "\t";
	}
	$evaluate = "Array\n$space(\n";
	$comma = $space;
	if(is_array($array)) {
		foreach($array as $key => $val) {
			$key = is_string($key) ? '\''.addcslashes($key, '\'\\').'\'' : $key;
			$val = !is_array($val) && (!preg_match("/^\-?[1-9]\d*$/", $val) || strlen($val) > 12) ? '\''.addcslashes($val, '\'\\').'\'' : $val;
			if(is_array($val)) {
				$evaluate .= "$comma$key => ".arrayeval($val, $level + 1);
			} else {
				$evaluate .= "$comma$key => $val";
			}
			$comma = ",\n$space";
		}
	}
	$evaluate .= "\n$space)";
	return $evaluate;
}
?>